package com.skit.common.event

import androidx.lifecycle.*
import org.greenrobot.eventbus.EventBus

/**
 * @author shuike <a href="mailto:shuike007@126.com">Contact me.</a>
 * @version 1.0
 * @since 2021/06/01
 */
class EventBusLifecycleObserver(owner: LifecycleOwner) : DefaultLifecycleObserver {
    init {
        owner.lifecycle.addObserver(this)
    }

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        EventBus.getDefault().apply {
            if (!isRegistered(owner)) register(owner)
        }
    }

    override fun onDestroy(owner: LifecycleOwner) {
        super.onDestroy(owner)
        EventBus.getDefault().apply {
            if (isRegistered(owner)) unregister(owner)
        }
    }

}